package com.yc.sakuralxm.uestcscanning

import android.app.Activity
import android.app.Instrumentation
import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.widget.TextView

import com.yc.sakuralxm.uestcscanning.tools.InputMethodUtil
import com.yc.sakuralxm.uestcscanning.tools.IntentUtil


/**
 *
 * Created by Administrator on 2016/8/5.
 */
abstract class BaseActivity : Activity() {

    protected var disInputMethodListener: View.OnClickListener? = null


    val activity: BaseActivity
        get() = this

    //    @RequiresApi(api = Build.VERSION_CODES.M)
    //    @Override
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //        MPermissions.requestPermissions(getActivity(), REQUECT_CODE_CALL_PHONE, Manifest.permission.CALL_PHONE);

        context = this

    }

    fun toSure(view: View) {
        IntentUtil.startActivity(activity, MainActivity::class.java)
    }

    fun toBack(view: View) {
        object : Thread() {
            override fun run() {
                val inst = Instrumentation()
                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK)
            }
        }.start()
    }

    protected fun addDisInputMethodListener(id: Int) {
        addDisInputMethodListener(findViewById(id))
    }

    protected fun addDisInputMethodListener(view: View?) {
        if (view != null) {
            if (disInputMethodListener == null) {
                disInputMethodListener = View.OnClickListener { InputMethodUtil.hide(activity) }
            }
            view.setOnClickListener(disInputMethodListener)
        }
    }

    override fun setTitle(title: CharSequence) {
        SetTitle(this, title)
    }

    fun setSureText(title: CharSequence) {
        SetSureText(this, title)
    }

    companion object {
        var context: Context? = null

        fun SetTitle(activity: Activity, title: CharSequence) {
            val view = activity.findViewById(R.id.idActionBarTv)
            if (view is TextView) {
                view.text = title
            }
        }

        fun setRig(activity: Activity, t: CharSequence) {
            val view = activity.findViewById(R.id.idActionBarSureTv)
            if (view is TextView) {
                view.text = t
            }
            if (!TextUtils.isEmpty(t)) {
                view.visibility = View.VISIBLE
            }
        }

        fun SetSureText(activity: Activity, title: CharSequence) {
            //        View view = activity.findViewById(R.id.idActionBarSureTv);
            //        if (view instanceof TextView) {
            //            ((TextView) view).setText(title);
            //        }
        }
    }
    //
}
